package com.example.demo.controller;

import com.example.demo.entity.Huisy;
import com.example.demo.service.HuisyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Optional;

@RestController
@RequestMapping("/api/huisy")
public class HuisyController {
    @Autowired
    private HuisyService huisyService;

    @PostMapping("/login")
    public ResponseEntity<Huisy> login(@RequestBody Huisy huisy) {
        Optional<Huisy> result = huisyService.login(huisy.getNum(), huisy.getPassword());
        return result.map(ResponseEntity::ok).orElseGet(() -> ResponseEntity.status(HttpStatus.UNAUTHORIZED).build());
    }

    @PostMapping("/register")
    public Huisy register(@RequestBody Huisy huisy) {
        return huisyService.register(huisy);
    }

    @PutMapping("/update")
    public Huisy update(@RequestBody Huisy huisy) {
        return huisyService.update(huisy);
    }
}
